package com.subway.babynote;

import com.subway.babynote.syncservice.syncapi;
/*水*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.database.Cursor;
import android.widget.SimpleAdapter;

public class feedcategory extends Activity {
	private static final int INVISIBLE = 0x00000004;
	NotedbHelper notedb;
	TextView notecomments_view, userage_view;
	Button login_button;
	//ListView lv1;
	FocusListView lv;
	int col;
	int mothermilk_id;
	int water_id;
	int milk_id;
	String[] food_array;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedcategory);
		setTitle(R.string.note);
		In = new Intent();

	  	//lv1 = (ListView) findViewById(R.id.list);
	 	lv=(FocusListView) findViewById(R.id.list);
	 	
		food_array = getResources().getStringArray(R.array.food_category);
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
				food_array));
	
		notecomments_view = (TextView) findViewById(R.id.notecomments_view);
		userage_view = (TextView) findViewById(R.id.userinfo_TextView);
		login_button = (Button) findViewById(R.id.login_button);
		userage_view.setText("");
		lv.setTextFilterEnabled(true);
		notedb = new NotedbHelper(this);
		try {
			if(syncapi.getSyncImpl().getUserid()!= -1){
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			mothermilk_id = syncapi.getSyncImpl().getFood(food_array[0]).food_id;
			water_id = syncapi.getSyncImpl().getFood(food_array[2]).food_id;
			int age = syncapi.getSyncImpl().getUserAge();			
			//should input user age
			if(age ==-1) {
				In.setClass(feedcategory.this, birth.class);
				startActivity(In);
			}
		
			if (!syncapi.getSyncImpl().getUserGender()) {
				userage_view.setText(getResources().getString(R.string.babygirl)+ age +getResources().getString(R.string.monthold));
			} else {
				userage_view.setText(getResources().getString(R.string.babyboy) + age + getResources().getString(R.string.monthold));
			}
		} catch (Exception e) {
			Log.v("feedcatogory", "from DB e=" + e.getMessage());
		}

		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				In.setClass(feedcategory.this, login.class);
				startActivity(In);
			}
		});
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Bundle bundle = new Bundle();
				switch (position) {
				// mothermilk
				case 0:
					bundle.putInt("food_id", mothermilk_id);
					bundle.putString("foodname", food_array[0]);
					In.setClass(feedcategory.this, gridviewinputamount.class);
					In.putExtras(bundle);
					startActivity(In);
					break;
				case 1:
					bundle.putString("foodname", food_array[1]);
					bundle.putString("from","feedcategory");
					bundle.putString("category", food_array[1]);
					In.setClass(feedcategory.this, choosebrand.class);
					In.putExtras(bundle);
					startActivity(In);

					break;
				// water
				case 2:

					bundle.putInt("food_id", water_id);
					bundle.putString("foodname", food_array[2]);
					In.setClass(feedcategory.this, inputamount.class);
					In.putExtras(bundle);
					startActivity(In);
					break;
				case 3:
					bundle.putString("foodname", food_array[3]);
					bundle.putString("category", food_array[3]);
					bundle.putString("from","feedcategory");
					In.setClass(feedcategory.this, choosebrand.class);
					In.putExtras(bundle);
					startActivity(In);
					break;
				case 4:
					bundle.putString("foodname", food_array[4]);
					bundle.putString("category", food_array[4]);
					bundle.putString("from","feedcategory");
					In.setClass(feedcategory.this, choosebrand.class);
					In.putExtras(bundle);
					startActivity(In);
					break;

				}
			}
		});

	}

	public void QuitApp() {
		new AlertDialog.Builder(feedcategory.this).setTitle(getResources().getString(R.string.prompt)).setMessage(
				getResources().getString(R.string.confirmMessage)).setIcon(R.drawable.btn_close_normal).setPositiveButton(getResources().getString(R.string.confirm),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {	
					
						finish();
					}
				}).setNegativeButton(getResources().getString(R.string.cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				}).show();

	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			QuitApp();
			return true;
			
		}
		return false;
	}
	
	public void onDestroy(){
		   notedb.close();
		   super.onDestroy();	  	   
		   
		}
}